﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace FSC.HaNamAPApplication
{
    public partial class HNAP_AdminOfficesEdit : System.Web.UI.UserControl
    {
        private string modeform
        {
            get
            {
                string modeform = "a";
                if (Request.QueryString["m"] != null && Request.QueryString["m"].ToString() != "")
                {
                    modeform = Request.QueryString["m"].ToString();
                }
                return modeform;
            }
        }

        private int officeId
        {
            get
            {
                int officeId = -1;
                if (Request.QueryString["oid"] != null && int.TryParse(Request.QueryString["oid"].ToString(), out officeId))
                {
                    officeId = int.Parse(Request.QueryString["oid"].ToString());
                }
                return officeId;
            }
        }

        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrop();

                if (this.modeform.Equals("u"))
                    LoadData();
            }
        }

        private void BindDrop()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            // Bind Offices
            var dataLevel = from o in db.Levels
                             orderby o.Level_ID
                             select new
                             {
                                 o.Level_ID,
                                 o.Name
                             };
            ddlLevel.DataValueField = "Level_ID";
            ddlLevel.DataTextField = "Name";
            ddlLevel.DataSource = dataLevel;
            ddlLevel.DataBind();

            ddlLevel.Items.Insert(0, new ListItem("--Chọn Cấp--", "-1"));
        }

        private void LoadData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            Office objOffice = db.Offices.SingleOrDefault(a => a.Office_ID == this.officeId);
            if (objOffice != null)
            {
                txtName.Text = objOffice.Name;
                ddlLevel.SelectedValue = objOffice.Level_ID.ToString();
            }
        }

        private void UpdateData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            Office objOffice;

            if (this.modeform.Equals("u"))
                objOffice = db.Offices.SingleOrDefault(a => a.Office_ID == this.officeId);
            else
                objOffice = new Office();

            if (objOffice == null) return;

            objOffice.Name = txtName.Text;
            objOffice.Level_ID = int.Parse(ddlLevel.SelectedValue);

            if (this.modeform.Equals("a"))
            {
                db.Offices.InsertOnSubmit(objOffice);
            }

            db.SubmitChanges();
            Response.Redirect(CurrentUrlWithoutParam + "?page=AdminOffices");
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            UpdateData();
        }
    }
}